import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rcParams

rcParams['font.family'] = 'times new roman'
rcParams['font.size'] = 15
rcParams['font.weight'] = 'normal'

'''
flow completion time(FCT)
Average FCTs
99th %-tile FCTs

(0,100KB]: Avg 
(0,100KB]: 99th Percentile 
(100KB,10MB]: Avg
(10MB,++): Avg
'''

numToRs = 64
numHosts = 10
numLinks = 10
numSLinks = 2

# workloads
flowsDir = '2/'
load = 0.6
time = 0.2
workloadCDFs = ['VL2']
workloadTypes = ['random']

# scheduler
schedulers = ['Detour']  # , ,'OvS'
iteration_a = 0
iteration_b = 1

overall_fct = []
for i in xrange(iteration_a, iteration_b):
    for workloadType in workloadTypes:
        for workloadCDF in workloadCDFs:
            for scheduler in schedulers:
                flowsFileName = "%s_%sToR_%sHosts_%sLinks_%sload_%stime_%s_%s_%s.txt" \
                                % (scheduler, numToRs, numHosts, numLinks, load, time, workloadType, workloadCDF, i)
                with open(flowsFileName, 'r') as f:
                    for line in f.readlines():
                        split = line.split()
                        overall_fct.append(float(split[5]) * 1000)
                    overall_fct.sort()
                    print len(overall_fct)

# overall average FCT
overall_sum_fct = 0
for x in overall_fct:
    overall_sum_fct += x
overall_avg_fct = overall_sum_fct / len(overall_fct)
print overall_sum_fct, overall_avg_fct

# 99th %-tile average FCT
tail99_fct = overall_fct[-len(overall_fct) / 100 + 1:]
tail99_sum_fct = 0
for x in tail99_fct:
    tail99_sum_fct += x
tail99_avg_fct = tail99_sum_fct / len(tail99_fct)
print tail99_sum_fct, tail99_avg_fct

fig = plt.figure(figsize=(5.8, 3.5))
ax = plt.axes()

cdfx = overall_fct
cdfy = np.linspace(1 / len(overall_fct), 1.0, len(overall_fct))

fs = 14
plt.semilogx(cdfx, cdfy, 'r', linewidth=1, label='Ideal')
plt.xlabel('FCT(ms)', fontsize=fs)
plt.ylabel('CDF', fontsize=fs)
plt.legend(loc=4, fontsize=fs)
plt.subplots_adjust(bottom=0.15, top=0.9, left=0.12, right=0.95)
plt.grid()
plt.show()
